package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ACategory;
import com.ruoyi.system.domain.AProduct;
import com.ruoyi.system.domain.vo.AProductVo;
import com.ruoyi.system.service.IACategoryService;
import com.ruoyi.system.service.IAProductService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品Controller
 *
 * @author 王家宁
 * @date 2024-08-14
 */
@Controller
@RequestMapping("/system/product")
public class AProductController extends BaseController {
    private String prefix = "system/product";

    @Autowired
    private IAProductService aProductService;
    @Autowired
    private IACategoryService categoryService;

    @GetMapping()
    public String product() {
        return prefix + "/product";
    }

    /**
     * 查询商品列表
     */
//    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AProduct aProduct) {
        startPage();
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        List<AProduct> collect = list.stream().filter(product -> product.getProductStatus() == 0)
                .collect(Collectors.toList());
        List<AProductVo> productVos = new ArrayList<>();
        collect.forEach(product -> {
            AProductVo aProductVo = new AProductVo();
            BeanUtils.copyProperties(product, aProductVo);
            ACategory one = categoryService.getOne(Wrappers.<ACategory>lambdaQuery()
                    .select(ACategory::getName)
                    .eq(ACategory::getId, product.getCategoryId())
            );
            aProductVo.setCategoryName(one.getName());
            productVos.add(aProductVo);
        });
        return getDataTable(productVos);
    }

    @PostMapping("/client/list")
    @ResponseBody
    public AjaxResult clientList(@RequestBody AProduct aProduct) {
        startPage();
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        List<AProductVo> productVos = new ArrayList<>();
        list.forEach(product -> {
            AProductVo aProductVo = new AProductVo();
            BeanUtils.copyProperties(product, aProductVo);
            ACategory one = categoryService.getOne(Wrappers.<ACategory>lambdaQuery()
                    .select(ACategory::getName)
                    .eq(ACategory::getId, product.getCategoryId())
            );
            aProductVo.setCategoryName(one.getName());
            productVos.add(aProductVo);
        });
        return AjaxResult.success(productVos);
    }
    /**
     * 导出商品列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AProduct aProduct) {
        List<AProduct> list = aProductService.selectAProductList(aProduct);
        ExcelUtil<AProduct> util = new ExcelUtil<AProduct>(AProduct.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AProduct aProduct) {
        return toAjax(aProductService.insertAProduct(aProduct));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("system:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AProduct aProduct = aProductService.selectAProductById(id);
        mmap.put("aProduct", aProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AProduct aProduct) {
        return toAjax(aProductService.updateAProduct(aProduct));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aProductService.deleteAProductByIds(ids));
    }
}
